package gsw.tool.skin.utils;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;

/**
 * 资源获取工具类
 */
public class SkinResources {
    private static SkinResources skinResources;
    private Resources appResources;
    private Resources newResources;
    private String skinPackageName;
    private boolean isDefaultSkin = true;

    private SkinResources(Context context) {
        appResources = context.getResources();
    }

    public static void init(Context context) {
        if (skinResources == null) {
            synchronized (SkinResources.class) {
                if (skinResources == null) {
                    skinResources = new SkinResources(context);
                }
            }
        }
    }

    public static SkinResources getInstance() {
        return skinResources;
    }

    /**
     * 设置皮肤
     *
     * @param resources
     * @param skinPackageName
     */
    public void applySkin(Resources resources, String skinPackageName) {
        this.newResources = resources;
        this.skinPackageName = skinPackageName;

        //是否使用默认皮肤
        isDefaultSkin = Tool.isEmpty(skinPackageName) || resources == null;
    }

    /**
     * 还原皮肤
     */
    public void reset() {
        this.newResources = null;
        this.skinPackageName = null;
        isDefaultSkin = true;
    }

    /**
     * 根据app中资源ID获取皮肤包中对应的资源ID
     *
     * @param resID
     * @return
     */
    public int getIdentifier(int resID) {
        if (isDefaultSkin) {
            return resID;
        }
        //资源名称
        String resName = appResources.getResourceEntryName(resID);
        //资源类型
        String resType = appResources.getResourceTypeName(resID);
        //新皮肤的ID
        int id = newResources.getIdentifier(resName, resType, skinPackageName);
        return id;
    }

    /**
     * 返回新皮肤的Color
     *
     * @param resId
     * @return
     */
    public int getColor(int resId) {
        if (isDefaultSkin) {
            return appResources.getColor(resId);
        }
        int skinId = getIdentifier(resId);
        if (skinId == 0) {
            return appResources.getColor(resId);
        }
        return newResources.getColor(skinId);
    }

    /**
     * 返回新皮肤的ColorStateList
     *
     * @param resId
     * @return
     */
    public ColorStateList getColorStateList(int resId) {
        if (isDefaultSkin) {
            return appResources.getColorStateList(resId);
        }
        int skinId = getIdentifier(resId);
        if (skinId == 0) {
            return appResources.getColorStateList(resId);
        }
        return newResources.getColorStateList(skinId);
    }

    /**
     * 返回新皮肤的Drawable
     *
     * @param resId
     * @return
     */
    public Drawable getDrawable(int resId) {
        //如果有皮肤  isDefaultSkin false 没有就是true
        if (isDefaultSkin) {
            return appResources.getDrawable(resId);
        }
        int skinId = getIdentifier(resId);
        if (skinId == 0) {
            return appResources.getDrawable(resId);
        }
        return newResources.getDrawable(skinId);
    }


    /**
     * 返回新皮肤的背景
     * 可能是Color 也可能是drawable
     *
     * @return
     */
    public Object getBackground(int resId) {
        String resourceTypeName = appResources.getResourceTypeName(resId);
        if (resourceTypeName.equals("color")) {
            return getColor(resId);
        } else {
            return getDrawable(resId);
        }
    }

    /**
     * 根据资源ID获取资源名
     *
     * @param resId
     * @return
     */
    public String getString(int resId) {
        try {
            if (isDefaultSkin) {
                return appResources.getString(resId);
            }
            int skinId = getIdentifier(resId);
            if (skinId == 0) {
                return appResources.getString(skinId);
            }
            return newResources.getString(skinId);
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 返回皮肤包的Typeface(字体)
     *
     * @param resId
     * @return
     */
    public Typeface getTypeface(int resId) {
        String skinTypefacePath = getString(resId);
        if (TextUtils.isEmpty(skinTypefacePath)) {
            return Typeface.DEFAULT;
        }
        try {
            Typeface typeface;
            if (isDefaultSkin) {
                typeface = Typeface.createFromAsset(appResources.getAssets(), skinTypefacePath);
                return typeface;
            }
            typeface = Typeface.createFromAsset(newResources.getAssets(), skinTypefacePath);
            return typeface;
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
        return Typeface.DEFAULT;
    }

}
